package com.mini.mysql.repository;



import com.mini.mysql.model.BusinessBaseInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author zhangxiaobin
 * @title: MiniLessonBaseInfoRepository
 * @description: TODO
 * @date 2019/10/2711:33
 */
@Repository
public interface BusinessBaseInfoRepository extends JpaRepository<BusinessBaseInfo, Long>, JpaSpecificationExecutor<BusinessBaseInfo> {

    @Query(value = "SELECT b.group_id ,group_concat(b.mini_lesson_id) lessonIds  FROM business_base_info b GROUP BY b.group_id",nativeQuery = true)
    List<Object[]> groupLessonData();

    List<BusinessBaseInfo> findAllByMiniLessonIdIn(Set<Long> ids);
    
    List<BusinessBaseInfo> findBySiteType(Integer siteType);

    List<BusinessBaseInfo> findAllByChannelSelfTypeAndSiteTypeAndFzIdAndChannelCodeAndLessonIdAndGroupIds(Integer channelSelfType, Integer siteType, Long fzId, String channelCode, String lessonId, String groupIds);

    List<BusinessBaseInfo> findAllByChannelSelfTypeAndSiteTypeAndFzIdAndChannelCodeAndSiteItemAndGroupIds(Integer channelSelfType, Integer siteType, Long fzId, String channelCode, Integer siteItem, String groupIds);

    @Query(value = "select a.lesson_id,b.lesson_name    from\n" +
            "(select lesson_id,max(mini_lesson_id) as mini_lesson_id from business_base_info where business_type=1\n" +
            "group by lesson_id) as a\n" +
            "inner join business_base_info as b\n" +
            "on a.mini_lesson_id=b.mini_lesson_id",nativeQuery = true)
    List<Object[]> getJTProductList();

    List<BusinessBaseInfo> findByUpdateTimeBetweenOrderByUpdateTimeAsc(Date startTime, Date endTime);

    List<BusinessBaseInfo> findByMiniLessonIdIn(List<Long> collect);

    BusinessBaseInfo findByMiniLessonId(Long miniLessonId);
}
